package com.xiaoyu.lanling.feature.chat.viewholder

import android.graphics.PorterDuff
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.emoji.widget.EmojiTextView
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.im.datamodel.MessagePayload
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageSystemItem
import com.xiaoyu.lanling.util.TextViewUtil.stripUnderlines

class ChatMessageSystemViewHolder : ChatMessageBaseViewHolder<ChatMessageSystemItem>() {

    private var mText: EmojiTextView? = null

    override fun doCreateView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_chat_admin, parent, false)
        mText = view?.findViewById(R.id.chat_message_admin)
        mText?.setOnClickListener(sOnItemClickListener)
        return view
    }

    override fun showData(position: Int, itemData: ChatMessageSystemItem) {
        mText?.apply {
            stripUnderlines(this, itemData.text)
            setTextColor(itemData.textColor)
            background.mutate().setColorFilter(itemData.backgroundColor, PorterDuff.Mode.DST)
            setViewTag(itemData)
        }
    }

    companion object {
        private val sOnItemClickListener = View.OnClickListener { v: View? ->
            val item: ChatMessageSystemItem = v.getViewTag<ChatMessageSystemItem>()
                    ?: return@OnClickListener
            if (item.action != MessagePayload.EMPTY_ACTION) {
                item.action.execute()
            }
        }
    }
}